<?php
	/*
	 * Stores model
	 * @author: Tran Van Thanh
	 * Email  : thanhtran@vietnambiz.com
	 * Date:	31/08/2011
	 */
class Stores_model extends MY_Model{

	function __construct()
	{
		parent::__construct();		
	}
	
	function get_all_products($limit = 1, $offset = 1)
	{
		$where = array(
			'a.status'	=> 'yes'
		);
        $q      =       $this->db->select('a.*,c.name as company_name')
                             ->from("products as a")                 
                 			 ->join('company as c','a.id_company = c.id')
                 			 ->where($where)                 			                     
                             ->limit($limit,$offset)
                             ->order_by('a.date_create DESC')
                             ->get();
        
        $result			= 	array();
        $result['rows'] 	=	$q->result();
        $q->free_result();

        $q      =	$this->db->select('count(a.id) as cnt')
                             ->from("products as a")
                             ->join('company as c','a.id_company = c.id')
                             ->where($where)
                             ->get();
        $result['num_rows']	=	$q->row()->cnt;
        $q->free_result();
        return	$result;
	}
	
	function get_all_products_by_price($price = null, $limit = 1, $offset = 1)
	{
		
		if($price === "0-5")
		{
			$where = array(
				'a.status'		=> 'yes',
				'a.price <='	=> 5000000
			);				
		}
		else if($price === "5-7")
		{
			$where = array(
				'a.status'		=> 'yes',
				'a.price >'		=> 5000000,
				'a.price <=' 	=> 7000000
			);				
		}
		else if($price === "7-10")
		{
			$where = array(
				'a.status'		=> 'yes',
				'a.price >'		=> 7000000,
				'a.price <=' 	=> 10000000
			);				
		}
		else if($price === "10-12")
		{				
			$where = array(
				'a.status'		=> 'yes',
				'a.price >'		=> 10000000,
				'a.price <='	=> 12000000
			);
		} 
		else if($price === "12-0")
		{				
			$where = array(
				'a.status'	=> 'yes',
				'a.price >'	=> "12000000"
			);
		}
		else
		{
			$price_arr = explode('-',$price);
			$from_price = $price_arr[0].'000000';
			$to_price	= $price_arr[1].'000000';
			
			$where = array(
				'a.status'		=> 'yes',
				'a.price >='	=> strip_tags($from_price),
				'a.price <='	=> strip_tags($to_price),
			);
		} 								

		$q      =       $this->db->select('a.*,c.name as company_name')
                             ->from("products as a")                 
                 			 ->join('company as c','a.id_company = c.id')
                 			 ->where($where)                 			                     
                             ->limit($limit,$offset)
                             ->order_by('a.date_create DESC')
                             ->get();
        
        $result			= 	array();
        $result['rows'] 	=	$q->result();
        $q->free_result();

        $q      =	$this->db->select('count(a.id) as cnt')
                             ->from("products as a")
                             ->join('company as c','a.id_company = c.id')
                             ->where($where)
                             ->get();
        $result['num_rows']	=	$q->row()->cnt;
        $q->free_result();
        return	$result;
	}
	
	function get_all_products_by_power($power = null, $limit = 1, $offset = 1)
	{		
		if($power === "0-9")
		{
			$where = array(
				'a.status'			=> 'yes',
				'a.cool_power <='	=> 9000
			);				
		}
		else if($power === "9-10")
		{
			$where = array(
				'a.status'			=> 'yes',
				'a.cool_power >'	=> 9000,
				'a.cool_power <=' 	=> 10000
			);				
		}
		else if($power === "10-12")
		{
			$where = array(
				'a.status'			=> 'yes',
				'a.cool_power >'	=> 10000,
				'a.cool_power <=' 	=> 12000
			);				
		}
		else if($power === "12-13")
		{				
			$where = array(
				'a.status'			=> 'yes',
				'a.cool_power >'	=> 12000,
				'a.cool_power <=' 	=> 13000
			);	
		} 
		else if($power === "13-18")
		{				
			$where = array(
				'a.status'			=> 'yes',
				'a.cool_power >'	=> 13000,
				'a.cool_power <=' 	=> 18000
			);
		} 	
		else if($power === "18-24")
		{				
			$where = array(
				'a.status'			=> 'yes',
				'a.cool_power >'	=> 18000,
				'a.cool_power <=' 	=> 24000
			);
		}
		else if($power === "24-30")
		{				
			$where = array(
				'a.status'			=> 'yes',
				'a.cool_power >'	=> 24000,
				'a.cool_power <=' 	=> 30000
			);
		}

		$q      =       $this->db->select('a.*,c.name as company_name')
                             ->from("products as a")                 
                 			 ->join('company as c','a.id_company = c.id')
                 			 ->where($where)                 			                     
                             ->limit($limit,$offset)
                             ->order_by('a.date_create DESC')
                             ->get();
        
        $result			= 	array();
        $result['rows'] 	=	$q->result();
        $q->free_result();

        $q      =	$this->db->select('count(a.id) as cnt')
                             ->from("products as a")
                             ->join('company as c','a.id_company = c.id')
                             ->where($where)
                             ->get();
        $result['num_rows']	=	$q->row()->cnt;
        $q->free_result();
        return	$result;
	}
	
	function get_all_products_by_company($id_company = null, $limit = 1, $offset = 1)
	{		
		$where = array(
			'a.status'		=> 'yes',
			'a.id_company'	=> $id_company
		);
        $q      =       $this->db->select('a.*,c.name as company_name')
                             ->from("products as a")                 
                 			 ->join('company as c','a.id_company = c.id')
                 			 ->where($where)                 			                     
                             ->limit($limit,$offset)
                             ->order_by('a.date_create DESC')
                             ->get();
        
        $result			= 	array();
        $result['rows'] 	=	$q->result();
        $q->free_result();

        $q      =	$this->db->select('count(a.id) as cnt')
                             ->from("products as a")
                             ->join('company as c','a.id_company = c.id')
                             ->where($where)
                             ->get();
        $result['num_rows']	=	$q->row()->cnt;
        $q->free_result();
        return	$result;
	}
	
	function get_all_products_by_utilities($id_utilities = null, $limit = 1, $offset = 1)
	{
		$where = array(
			'a.status'			=> 'yes',
			'a.id_utilities'	=> $id_utilities
		);
        $q      =       $this->db->select('a.*,c.name as company_name')
                             ->from("products as a")                 
                 			 ->join('company as c','a.id_company = c.id')
                 			 ->where($where)                 			                     
                             ->limit($limit,$offset)
                             ->order_by('a.date_create DESC')
                             ->get();
        
        $result			= 	array();
        $result['rows'] 	=	$q->result();
        $q->free_result();

        $q      =	$this->db->select('count(a.id) as cnt')
                             ->from("products as a")
                             ->join('company as c','a.id_company = c.id')
                             ->where($where)
                             ->get();
        $result['num_rows']	=	$q->row()->cnt;
        $q->free_result();
        return	$result;
	}
	
	function search_products_by_keywords($keywords = null, $limit = 1, $offset = 1)
	{
		$where = array(
			'a.status'			=> 'yes',			
		);
        $q      =       $this->db->select('a.*,c.name as company_name')
                             ->from("products as a")                 
                 			 ->join('company as c','a.id_company = c.id')
                 			 ->where($where)      
                 			 ->like('a.name',$keywords)
                    		 ->or_like('a.code',$keywords)
                    		 ->or_like('a.description',$keywords)            			                     
                    		 ->or_like('c.name',$keywords)
                             ->limit($limit,$offset)
                             ->order_by('a.date_create DESC')
                             ->get();
        
        $result			= 	array();
        $result['rows'] 	=	$q->result();
        $q->free_result();

        $q      =	$this->db->select('count(a.id) as cnt')
                             ->from("products as a")
                             ->join('company as c','a.id_company = c.id')
                             ->where($where)
                             ->like('a.name',$keywords)
                    		 ->or_like('a.code',$keywords)
                    		 ->or_like('a.description',$keywords)            			                     
                    		 ->or_like('c.name',$keywords)
                             ->get();
        $result['num_rows']	=	$q->row()->cnt;
        $q->free_result();
        return	$result;
	}
	
	function get_products_info_by_id($id_product = null)
	{
		$this->db->select("a.*")
                 ->from("products as a")               
                 ->where('a.id',$id_product); 
        $q = $this->db->get();
        $result = $q->row();
        $q->free_result();        
        return $result; 
	}
	
	function get_products_orther_power_cool($cool_power = null,$id_products_current = null)
	{
		$this->db->select()
                 ->from("products")
                 ->where('cool_power',$cool_power)
                 ->where('id !=',$id_products_current); 
        $q = $this->db->get();
        $result = $q->result();
        $q->free_result();        
        return $result; 
	}
	
	function get_company_info_by_name_link($name_link = null)
	{
		$query = $this->db->where('name_link', $name_link)->get("company");
        if($query->num_rows() > 0) {
            $data = $query->row();
        }
        return $data;
	}
	
	function get_utilities_info_by_name_link($name_link = null)
	{
		$query = $this->db->where('name_link', $name_link)->get("utilities");
        if($query->num_rows() > 0) {
            $data = $query->row();
        }
        return $data;
	}
	
	function get_questions_with_products($id_product = null)
	{
		$this->db->select("b.id,b.title,b.title_link")
	                 ->from("questions_products as a")
	                 ->join("questions as b","a.id_question = b.id")
	                 ->join("products as c","c.id = a.id_product")
	                 ->where("c.id",$id_product);	                 		                                 
        $q = $this->db->get();
        $result = $q->result();
        $q->free_result();
        return $result;	
	}
	
	function get_articles_with_products($id_product = null)
	{				
		$this->db->select("b.id,b.title,b.title_link,b.description")
	                 ->from("articles_products as a")
	                 ->join("articles as b","a.id_articles = b.id")
	                 ->join("products as c","c.id = a.id_product")
	                 ->where("c.id",$id_product);	                 		                                 
        $q = $this->db->get();
        $result = $q->result();
        $q->free_result();
        return $result;				      
	}
    
    function get_other_products($id)
    {
        $result = array();
        $row = $this->db->select()->from("products")->where('id',$id)->get()->row();
        if($row){
            $this->db->select()
                     ->from("products")
                     ->where('id_product_type',$row->id_product_type)
                     ->where('id !=',$id)
                     ->limit(8); 
            $q = $this->db->get();
            $result = $q->result();
            $q->free_result();  
        }
        
        return $result;
    }
	
}